﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using hospital_registration_system.Entity;
using LinkSql;

namespace hospital_registration_system.DAL
{
    public class AppraiseDao
    {
        private string tableName = "appraise";
        private string id= "id";         // id号
        private string CREATED_BY= "CREATED_BY";  // 创建人
        private string CREATED_TIME= "CREATED_TIME";// 创建时间
        private string UPDATED_BY= "UPDATED_BY";  // 更新人
        private string UPDATED_TIME= "UPDATED_TIME";// 更新时间
        private string patient_id= "patient_id";  // 病人id
        private string state= "state";      // 状态
        private string content= "content"; // 内容
        private string star_level= "star_level";// 星级
        private string record_id= "record_id";//就诊记录id

        /// <summary>
        /// 通过对象插入
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public bool insert(Appraise a)
        {
            Dictionary<string, Object> map = new Dictionary<string, object>();
            map.Add(CREATED_BY, a.CREATED_BY1);
            map.Add(CREATED_TIME, a.CREATED_TIME1);
            map.Add(UPDATED_BY, a.UPDATED_BY1);
            map.Add(UPDATED_TIME, a.UPDATED_TIME1);
            map.Add(patient_id, a.Patient_id);
            map.Add(state, a.State);
            map.Add(content, a.Content);
            map.Add(star_level, a.Star_level);
            map.Add(record_id, a.Record_id);
            return MySQLUtils.insert(tableName, map);
        }
        /// <summary>
        /// 通过id删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool delete(int id)
        {
            return MySQLUtils.delete(tableName, id);
        }

        /// <summary>
        /// 通过对象修改
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        public bool update(Appraise a)
        {
            Dictionary<string, Object> map = new Dictionary<string, object>();
            map.Add(id, a.Id);
            map.Add(CREATED_BY, a.CREATED_BY1);
            map.Add(CREATED_TIME, a.CREATED_TIME1);
            map.Add(UPDATED_BY, a.UPDATED_BY1);
            map.Add(UPDATED_TIME, a.UPDATED_TIME1);
            map.Add(patient_id, a.Patient_id);
            map.Add(state, a.State);
            map.Add(content, a.Content);
            map.Add(star_level, a.Star_level);
            map.Add(record_id, a.Record_id);
            return MySQLUtils.update(tableName, map);
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public List<Appraise> selectAll()
        {
            return GetAppraisesByDataTable(MySQLUtils.selectAll(tableName));
        }

        /// <summary>
        /// 通过id查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Appraise> selectById(int id)
        {
            return GetAppraisesByDataTable(MySQLUtils.selectById(tableName,id));
        }

        /// <summary>
        /// 通过map查询
        /// </summary>
        /// <param name="map"></param>
        /// <returns></returns>
        public List<Appraise> selectByMap(Dictionary<string, Object> map)
        {
            return GetAppraisesByDataTable(MySQLUtils.selectByMap(tableName,map));
        }

        /// <summary>
        /// 通过病人id查找
        /// </summary>
        /// <param name="patient_id"></param>
        /// <returns></returns>
        public List<Appraise> selectBypatient_id(int patient_id)
        {
            Dictionary<string, Object> map = new Dictionary<string, object>();
            map.Add(this.patient_id, patient_id);
            return GetAppraisesByDataTable(MySQLUtils.selectByMap(tableName, map));
        }

        public List<Appraise> GetAppraisesByDataTable(DataTable dataTable)
        {
            List<Appraise> ret = new List<Appraise>();
            foreach(DataRow row in dataTable.Rows)
            {
                ret.Add(new Appraise(
                    int.Parse(row["id"].ToString()),
                    row["created_by"].ToString(),
                    (DateTime)(row["created_time"]),
                    row["updated_by"].ToString(),
                    (DateTime)(row["updated_time"]),
                    int.Parse(row["patient_id"].ToString()),
                    int.Parse(row["state"].ToString()),
                    row["content"].ToString(),
                    (decimal)(row[star_level]),
                    int.Parse(row["record_id"].ToString())
                ));
            }
            return ret;
        }
    }
}
